import json
import sys
import time

import pandas as pd
import requests
import san

sys.path.append('..')
from tools.wrappers import deco_retry


@deco_retry(retry=15, retry_sleep=3)
def get_jinse_bulletin_by_id(id, dataframe=False):
    header = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36',
        'cookie': 'userId=eyJpdiI6Ikt5a0JiaWUra0tiZllPVkxjYlhvM2c9PSIsInZhbHVlIjoiU21pZkI2dzV0QzhVMjdOcVg2Q3FFVnVhU3VLMTRVbFwvRlVZYXQwWWd5UEJmMkVWNTJ3UEdYZ1wvYUVTaHBleDhJcGJxa3k2WHNnR3RrYjlZM0pXM1c3UT09IiwibWFjIjoiZDQyNzc2M2RhY2EzZjI0MzI2YmJiYjM5ZDA5YTQwZWRhYmI2YTE5ODgyZDI0ZWI2ZmUxZDFmNjY2ZGNiOGZhOCJ9; is_refresh=eyJpdiI6Ikhra0huSnpxU2dJRWFiZVFCRDQzbEE9PSIsInZhbHVlIjoiejRNcWpCUVdQR2xaMUJ0N2VaOFNBZz09IiwibWFjIjoiY2JjZmZiZWMyMjkzYzNiNDVhNjQ3M2IxYTZmZjVhYzY3MDFiYmY4MTA3YjFhMmVhMWI5ZGVlZGY2MGE0MDU4NSJ9; Hm_lvt_3b668291b682e6dc69686a3e2445e11d=1669185149,1670309203,1670310605; Hm_lvt_56b68a6c42577e34b7c82cbd85f82c52=1669185149,1670309203,1670310605; Hm_lpvt_56b68a6c42577e34b7c82cbd85f82c52=1670311055; Hm_lpvt_3b668291b682e6dc69686a3e2445e11d=1670311055'}

    url = f'https://api.jinse.cn/noah/v2/live/{id}'
    res = requests.get(url, headers=header, timeout=15)
    data = json.loads(res.text)
    if 'id' not in data:
        data = {}
    if dataframe:
        return pd.DataFrame(data)
    else:
        return data


@deco_retry(retry=15, retry_sleep=3)
def get_aicoin_bulletin_by_last_id(last_id, dataframe=False):
    header = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36',
        'referer': 'https://www.aicoin.com/?long_live_aicoin=true',
        'accept-encoding': 'gzip, deflate, br',
        'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
        'x-requested-with': 'XMLHttpRequest',
        'x-xsrf-token': 'eyJpdiI6IlcrRWk5dWFEajBPME8zRk5XRFBvYVE9PSIsInZhbHVlIjoiNlBiMXh2UmhaQzZVcVwvajVrYkw5Vk81bVwvWDNCMDcrbkJSbE1zRVYzU3ZHNmMxNEc5ZWU4OUJNN0lpVVNKMlErVWhuUUxkWnZCNnpPcXJ3WVNIRDhYdz09IiwibWFjIjoiOGQ4MDIwNzU1N2VjYmQ3MzEzYTY5MGU4Y2UzYzBiY2M4ZGUzNGZjNDg0Yzk3MjFlNGJhYTk2OTA0NjJmZDAyZCJ9',
        'cookie': 'Hm_lvt_3c606e4c5bc6e9ff490f59ae4106beb4=1670386124; _pk_id.2.2253=8767976bf2f08a57.1670386124.; _ga=GA1.2.1895126092.1670386124; _gid=GA1.2.1466404444.1670386124; __gads=ID=6076f0c35e74f145-2236f903c2d8007b:T=1670386124:RT=1670386124:S=ALNI_MaSeuKrIY_bky5Qay-k_gFw_0Fhtg; __gpi=UID=00000b8b8554d4d8:T=1670386124:RT=1670386124:S=ALNI_MYCDBMWdxYwRQIrPeGDt_6sLrjGeg; _pk_ref.2.e882=["","",1670391287,"https://www.aicoin.com/?lang=zh&long_lives_aicoin=\"live\""]; _pk_id.2.e882=89c026319d322b78.1670391287.; _pk_ses.2.e882=1; _gat_gtag_UA_108140256_2=1; _iidt=rIExq6ZV5rEt/VkBKm6SwK8weslHjkixKyC1wFtsvQxxE3JVjplOc3jIjbYf3yNaKURwODoZKHmasQ==; _vid_t=EC6xVHkkgSdA5nlqCpFXE455VU9Yf7S1clZzkntx9J8Z1wAXqYoPMZwV8jN3+Y5iFwrtI0v67lC6Pg==; XSRF-TOKEN=eyJpdiI6IlcrRWk5dWFEajBPME8zRk5XRFBvYVE9PSIsInZhbHVlIjoiNlBiMXh2UmhaQzZVcVwvajVrYkw5Vk81bVwvWDNCMDcrbkJSbE1zRVYzU3ZHNmMxNEc5ZWU4OUJNN0lpVVNKMlErVWhuUUxkWnZCNnpPcXJ3WVNIRDhYdz09IiwibWFjIjoiOGQ4MDIwNzU1N2VjYmQ3MzEzYTY5MGU4Y2UzYzBiY2M4ZGUzNGZjNDg0Yzk3MjFlNGJhYTk2OTA0NjJmZDAyZCJ9; aicoin_token=zKuTGf7MXI9T66KAduEpJC1xgL84kaw5BJLOwBE38FsF7uMOrFu9DymrJrCQlbtjdh5wovzeHBRzLSlgcCy3XQLUabpL+ZcupRRlKE/DDsL0c9eIblYtrd86tqMZQzM90NMn23F4SMHGyTQ27JLVyVoaOOCh+DZlW1toyWeFMfL7a1V7eobg6dMUlRrdSLx/iGt0SHlA6gVm8b96X8jBNuFp33gV9nj4OVt70gJ/i/Q=; __els__=1; _pk_ref.2.2253=["","",1670391311,"https://www.google.com/"]; _pk_ses.2.2253=1; Hm_lpvt_3c606e4c5bc6e9ff490f59ae4106beb4=1670391311; aicoin_session=eyJpdiI6IkZVSm5kemVkUFwvWGlYV2xLYjdEdnVnPT0iLCJ2YWx1ZSI6InNKK1wvRmRsWHViRGxnN3JQUkRwd1A5eGZDUklRalVYV25NMldxYStXUWIyZGcxMHorY3hibjhyWmt2aWxJa1wvMm44dFY1QURcL09aNWE2RkJCa2NsZXlRPT0iLCJtYWMiOiJiYjMwNjRkODFhNjU0NzA4N2IwNDYwNDkwYzM2NjRkYTdhNzE1NjRhODdlMzAyNTZiYWUzMWM5NTVkYTk3MzgyIn0='
    }
    url = f'https://www.aicoin.com/api/chart/kline/common/flash?last_id={last_id}'
    res = requests.get(url=url, headers=header, timeout=15)
    data = json.loads(res.text)['data']['list']
    if res.status_code == 200 and data:
        if dataframe:
            return pd.DataFrame(data)
        else:
            return data
    else:
        if res.status_code != 200:
            print(f'id={last_id}之后的快讯获取异常： {res.text}')
        if res.status_code == 200 and not data:
            print(f'快讯已经获取到头')
        if dataframe:
            return pd.DataFrame()
        else:
            return []


@deco_retry(retry=15, retry_sleep=3)
def get_aicoin_bulletin_last_id():
    header = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36',
        'referer': 'https://www.aicoin.com/?long_live_aicoin=true',
        'accept-encoding': 'gzip, deflate, br',
        'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
        'x-requested-with': 'XMLHttpRequest',
        'x-xsrf-token': 'eyJpdiI6IlcrRWk5dWFEajBPME8zRk5XRFBvYVE9PSIsInZhbHVlIjoiNlBiMXh2UmhaQzZVcVwvajVrYkw5Vk81bVwvWDNCMDcrbkJSbE1zRVYzU3ZHNmMxNEc5ZWU4OUJNN0lpVVNKMlErVWhuUUxkWnZCNnpPcXJ3WVNIRDhYdz09IiwibWFjIjoiOGQ4MDIwNzU1N2VjYmQ3MzEzYTY5MGU4Y2UzYzBiY2M4ZGUzNGZjNDg0Yzk3MjFlNGJhYTk2OTA0NjJmZDAyZCJ9',
        'cookie': 'Hm_lvt_3c606e4c5bc6e9ff490f59ae4106beb4=1670386124; _pk_id.2.2253=8767976bf2f08a57.1670386124.; _ga=GA1.2.1895126092.1670386124; _gid=GA1.2.1466404444.1670386124; __gads=ID=6076f0c35e74f145-2236f903c2d8007b:T=1670386124:RT=1670386124:S=ALNI_MaSeuKrIY_bky5Qay-k_gFw_0Fhtg; __gpi=UID=00000b8b8554d4d8:T=1670386124:RT=1670386124:S=ALNI_MYCDBMWdxYwRQIrPeGDt_6sLrjGeg; _pk_ref.2.e882=["","",1670391287,"https://www.aicoin.com/?lang=zh&long_lives_aicoin=\"live\""]; _pk_id.2.e882=89c026319d322b78.1670391287.; _pk_ses.2.e882=1; _gat_gtag_UA_108140256_2=1; _iidt=rIExq6ZV5rEt/VkBKm6SwK8weslHjkixKyC1wFtsvQxxE3JVjplOc3jIjbYf3yNaKURwODoZKHmasQ==; _vid_t=EC6xVHkkgSdA5nlqCpFXE455VU9Yf7S1clZzkntx9J8Z1wAXqYoPMZwV8jN3+Y5iFwrtI0v67lC6Pg==; XSRF-TOKEN=eyJpdiI6IlcrRWk5dWFEajBPME8zRk5XRFBvYVE9PSIsInZhbHVlIjoiNlBiMXh2UmhaQzZVcVwvajVrYkw5Vk81bVwvWDNCMDcrbkJSbE1zRVYzU3ZHNmMxNEc5ZWU4OUJNN0lpVVNKMlErVWhuUUxkWnZCNnpPcXJ3WVNIRDhYdz09IiwibWFjIjoiOGQ4MDIwNzU1N2VjYmQ3MzEzYTY5MGU4Y2UzYzBiY2M4ZGUzNGZjNDg0Yzk3MjFlNGJhYTk2OTA0NjJmZDAyZCJ9; aicoin_token=zKuTGf7MXI9T66KAduEpJC1xgL84kaw5BJLOwBE38FsF7uMOrFu9DymrJrCQlbtjdh5wovzeHBRzLSlgcCy3XQLUabpL+ZcupRRlKE/DDsL0c9eIblYtrd86tqMZQzM90NMn23F4SMHGyTQ27JLVyVoaOOCh+DZlW1toyWeFMfL7a1V7eobg6dMUlRrdSLx/iGt0SHlA6gVm8b96X8jBNuFp33gV9nj4OVt70gJ/i/Q=; __els__=1; _pk_ref.2.2253=["","",1670391311,"https://www.google.com/"]; _pk_ses.2.2253=1; Hm_lpvt_3c606e4c5bc6e9ff490f59ae4106beb4=1670391311; aicoin_session=eyJpdiI6IkZVSm5kemVkUFwvWGlYV2xLYjdEdnVnPT0iLCJ2YWx1ZSI6InNKK1wvRmRsWHViRGxnN3JQUkRwd1A5eGZDUklRalVYV25NMldxYStXUWIyZGcxMHorY3hibjhyWmt2aWxJa1wvMm44dFY1QURcL09aNWE2RkJCa2NsZXlRPT0iLCJtYWMiOiJiYjMwNjRkODFhNjU0NzA4N2IwNDYwNDkwYzM2NjRkYTdhNzE1NjRhODdlMzAyNTZiYWUzMWM5NTVkYTk3MzgyIn0='
    }
    url = f'https://www.aicoin.com/api/chart/kline/common/flash?first_id='
    res = requests.get(url=url, headers=header, timeout=15)
    data = json.loads(res.text)['data']['list']
    return pd.DataFrame(data)['id'].max()


def get_metrics_list(asset=None, specific_metric=None):
    if asset is None:
        metrics_list = san.available_metrics()
    else:
        metrics_list = san.available_metrics_for_slug(slug=asset)

    metrics_list = list(set(metrics_list))
    metrics_list.sort()

    if specific_metric is not None:
        res = []
        for metrics in metrics_list:
            if specific_metric in metrics:
                res.append(metrics)
        return res

    return metrics_list


def get_social_metrics_list(asset=None):
    metrics_list = get_metrics_list(asset=asset)
    social_metrics_list = []
    for metric in metrics_list:
        if 'twitter' in metric:
            social_metrics_list.append(metric)
        elif 'social' in metric:
            social_metrics_list.append(metric)
        elif 'community' in metric:
            social_metrics_list.append(metric)
        elif 'github' in metric:
            social_metrics_list.append(metric)
        elif 'sentiment' in metric:
            social_metrics_list.append(metric)
    if asset == 'bitcoin':
        if 'social_active_users' in social_metrics_list:
            social_metrics_list.remove('social_active_users')  # 该指标获取不到数据
        if 'nft_social_volume' in social_metrics_list:
            social_metrics_list.remove('nft_social_volume')  # 该指标获取不到数据
    social_metrics_list.sort()
    return social_metrics_list


def get_indicators_via_http(indic_name=None, asset='bitcoin', start_date=None, end_date=None, interval='1d',
                            test_times=5, **kwargs):
    header = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36'}
    Cookie = {
        '_sanbase_sid': 'SFMyNTY.g3QAAAADbQAAAAxhY2Nlc3NfdG9rZW5tAAABvmV5SmhiR2NpT2lKSVV6VXhNaUlzSW5SNWNDSTZJa3BYVkNKOS5leUpoZFdRaU9pSn'
                        'pZVzUwYVcxbGJuUWlMQ0pqYkdsbGJuUWlPaUpEYUhKdmJXVWdNVEF4TGpBdU1DNHdJaXdpWlhod0lqb3hOalUzTURFeU5UazBMQ0pwWVhRaU9q'
                        'RTJOVGN3TVRJeU9UUXNJbWx6Y3lJNkluTmhiblJwYldWdWRDSXNJbXAwYVNJNklqYzNabU5sWWpoaExURXdObVV0TkRrMFlpMDVaamcyTFRCal'
                        'l6Wm1ORE0yWVRjNFlpSXNJbTVpWmlJNk1UWTFOekF4TWpJNU15d2ljR3hoZEdadmNtMGlPaUpYYVc1a2IzZHpJREV3SWl3aWMzVmlJam9pTVRj'
                        'MU1Ea2lMQ0owZVhBaU9pSmhZMk5sYzNNaUxDSjBlWEJsSWpvaWRYTmxjbDloWTJObGMzTmZkRzlyWlc0aWZRLlg1UnB0OHZFZTFQRmJ1Tl93eE'
                        'J0V1hzOFFUODMxdXlDOUpKWlJxT003cFl2ZG5SeEg0bV9vbzdDODJEU3dJNjZQYVktTW1mcGxfdDhid0hDdlJuZ09nbQAAAAphdXRoX3Rva2Vu'
                        'bQAAAb5leUpoYkdjaU9pSklVelV4TWlJc0luUjVjQ0k2SWtwWFZDSjkuZXlKaGRXUWlPaUp6WVc1MGFXMWxiblFpTENKamJHbGxiblFpT2lKRG'
                        'FISnZiV1VnTVRBeExqQXVNQzR3SWl3aVpYaHdJam94TmpVM01ERXlOVGswTENKcFlYUWlPakUyTlRjd01USXlPVFFzSW1semN5STZJbk5oYm5S'
                        'cGJXVnVkQ0lzSW1wMGFTSTZJamMzWm1ObFlqaGhMVEV3Tm1VdE5EazBZaTA1WmpnMkxUQmpZelptTkRNMllUYzRZaUlzSW01aVppSTZNVFkxTn'
                        'pBeE1qSTVNeXdpY0d4aGRHWnZjbTBpT2lKWGFXNWtiM2R6SURFd0lpd2ljM1ZpSWpvaU1UYzFNRGtpTENKMGVYQWlPaUpoWTJObGMzTWlMQ0ow'
                        'ZVhCbElqb2lkWE5sY2w5aFkyTmxjM05mZEc5clpXNGlmUS5YNVJwdDh2RWUxUEZidU5fd3hCdFdYczhRVDgzMXV5QzlKSlpScU9NN3BZdmRuUn'
                        'hING1fb283QzgyRFN3STY2UGFZLU1tZnBsX3Q4YndIQ3ZSbmdPZ20AAAANcmVmcmVzaF90b2tlbm0AAAHAZXlKaGJHY2lPaUpJVXpVeE1pSXNJ'
                        'blI1Y0NJNklrcFhWQ0o5LmV5SmhkV1FpT2lKellXNTBhVzFsYm5RaUxDSmpiR2xsYm5RaU9pSkRhSEp2YldVZ01UQXhMakF1TUM0d0lpd2laWG'
                        'h3SWpveE5qVTVORE14TkRrMExDSnBZWFFpT2pFMk5UY3dNVEl5T1RRc0ltbHpjeUk2SW5OaGJuUnBiV1Z1ZENJc0ltcDBhU0k2SWpKbVpqVTFN'
                        'VGN4TFdabU1qUXRORGs1T0MwNU9EY3pMVE5sTkdFeFpHRXpNVEF5WWlJc0ltNWlaaUk2TVRZMU56QXhNakk1TXl3aWNHeGhkR1p2Y20waU9pSl'
                        'hhVzVrYjNkeklERXdJaXdpYzNWaUlqb2lNVGMxTURraUxDSjBlWEFpT2lKeVpXWnlaWE5vSWl3aWRIbHdaU0k2SW5WelpYSmZjbVZtY21WemFG'
                        'OTBiMnRsYmlKOS44T1lJLWc3T1M2MWxxakdaV2pPUUdhcTFHLXVZMG9vV3Y4djEwa3R0Q2Z3bVhyVjNRRjB6bk00SXhlbkJ5YzdxMTlieEtCLX'
                        'BadUNTT0tOb3dnd2VFUQ.Uolsu2HubKiC871vzjq8D_UBiq8eOZfQN7YIjfCWmz0'}
    post_json = {
        'query': """{getMetric(metric: """ + f'"{indic_name}"' + """){
        timeseriesData(
          slug: """ + f'"{asset}"' + """
          from: """ + f'"{start_date}' + """T00:00:01Z"
          to: """ + f'"{end_date}' + """T23:59:59Z"
          interval: """ + f'"{interval}"' + """){
            datetime
            value
          }
      }
    }"""}

    url = "https://api.santiment.net/graphiql?"
    ii = 1
    while ii <= test_times:  # 最多重连test_times次
        res = requests.post(url, headers=header, data=post_json, timeout=30, cookies=Cookie)
        if res.status_code == 200:
            if 'errors' in res.text:
                print(f'指标{indic_name}数据获取失败，{res.status_code}; {res.text}')
                time.sleep(5)
                ii += 1
            else:
                break  # 链接成功，不需继续尝试链接
        else:
            print(f'指标{indic_name}数据获取失败，{res.status_code}; {res.text}')
            time.sleep(5)
            ii += 1
    if (res.status_code != 200) or ('errors' in res.text):
        raise RuntimeError(f'已经重连了{test_times}次，还是无法成功链接，请检查查询语句、网络链接或账户权限')

    try:
        data = json.loads(res.text, )
        data = data['data']['getMetric']['timeseriesData']
        columns = list(set(data[0].keys()))
        df = pd.DataFrame.from_records(data, columns=columns)
        if 's' in interval or 'm' in interval or 'h' in interval:  # 如果数据频率为秒，分，时
            df['datetime'] = df['datetime'].apply(lambda x: x[:10] + ' ' + x[11:19])
        else:
            df['datetime'] = df['datetime'].apply(lambda x: x[:10])
    except Exception:  # 其他未知错误
        print(f'指标{indic_name}数据获取正常，但转换成Dataframe时失败')
        return None

    df.rename(columns={'datetime': 'end_date'}, inplace=True)
    df.drop_duplicates(subset=['end_date'], inplace=True)  # 同一天多条记录进行去重
    df.set_index('end_date', inplace=True)
    if len(df.columns) == 1:
        df.columns = [f'{indic_name}']
    else:
        print(f'指标{indic_name}数据为多列，无法对指标列重命名')
    print(f'指标{indic_name}数据获取成功')
    return df

